package controllers;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.alibaba.fastjson.JSONObject;

import cn.org.rapid_framework.page.Page;
import models.model.Commoditys;
import models.model.Messages;
import models.model.MltwoLevel;
import models.model.News;
import models.service.CommoditysService;
import models.service.IntroducesService;
import models.service.MessagesService;
import models.service.MltwoLevelService;
import models.service.NewsService;
import models.vo.query.CommoditysQuery;
import models.vo.query.IntroducesQuery;
import models.vo.query.MessagesQuery;
import models.vo.query.MltwoLevelQuery;
import models.vo.query.NewsQuery;
import play.Play;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.results.RenderText;

public class Application extends Controller {
	
	//图片的全局路径
	public static String IMG_PATH;
	

	// 商品的二级目录
	private static MltwoLevelService mltwoLevelService;

	// 商品信息
	private static CommoditysService commoditysService;
	
	private static MessagesService  messagesService;
	//品牌优势
	public static IntroducesService  introducesService;
	public static NewsService  newsService;
	
	public static String COMPANYPATH;//公司信息json文件路径
	public static String LUNBOPATH;//轮播信息json文件路径
	public static String PPYSPATH;//品牌优势json文件路径
	public static String COMMODITYPATH;//首页商品json文件路径
	static {
		IMG_PATH = "http://industrial.b0.upaiyun.com/";
		mltwoLevelService = (MltwoLevelService) play.modules.spring.Spring.getBean("mltwoLevelService");
		commoditysService = (CommoditysService) play.modules.spring.Spring.getBean("commoditysService");
		messagesService=(MessagesService)play.modules.spring.Spring.getBean("messagesService");
		introducesService = (IntroducesService)play.modules.spring.Spring.getBean("introducesService");
		newsService = (NewsService)play.modules.spring.Spring.getBean("newsService");
		COMPANYPATH=System.getProperty("user.dir")+"/public/datas/companyInfo.json";
		PPYSPATH=System.getProperty("user.dir")+"/public/datas/PPYSInfo.json";
		LUNBOPATH=System.getProperty("user.dir")+"/public/datas/LUNBOInfo.json";
		COMMODITYPATH=System.getProperty("user.dir")+"/public/datas/communityInfo.json";
	}
	

	public static void index() {
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		//读取轮播信息
		String LUBBOJson = common.JsonFile.jsonUtil.ReadFile(LUNBOPATH);
		JSONObject LunBOInfo=JSONObject.parseObject(LUBBOJson);
		
		
		
		
		
		//读取商品信息
		
		CommoditysQuery queryCom = new CommoditysQuery();
		queryCom.setMainShow("1");
		Page comPage = commoditysService.findPage(queryCom);

		
		//读取品牌优势信息
		
		IntroducesQuery queryInt = new IntroducesQuery();
		Page intPage = introducesService.findPage(queryInt);
		
		//读取新闻
		NewsQuery query =new NewsQuery();
		query.setIsShow("1");
		Page newPage = newsService.findPage(query);
		
		
		
		render(companyInfo,LunBOInfo,comPage,intPage,newPage);
	}

	
	
	
	public static void about() {
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		String[] detailsp = companyInfo.get("companyDetail").toString().split("。");
		List<String> asList = Arrays.asList(detailsp);
		render(companyInfo,asList);
	}

	
	
	public static void brand() {
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		
		
		//读取品牌优势信息
		IntroducesQuery queryInt = new IntroducesQuery();
		Page intPage = introducesService.findPage(queryInt);
		render(companyInfo,intPage);
	}

	
	
	public static void company() {
		//读取公司信息
				String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
				JSONObject companyInfo=JSONObject.parseObject(companyJson);
		render(companyInfo);
	}

	public static void join() {
		//读取公司信息
				String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
				JSONObject companyInfo=JSONObject.parseObject(companyJson);
		render(companyInfo);
	}

	public static void pro_d(int id) {
		Commoditys  entity=new Commoditys();
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		// 获取所有的类型
		MltwoLevelQuery query1 = new MltwoLevelQuery();
		List<MltwoLevel> result = mltwoLevelService.findPage(query1).getResult();
		//获取商品
		CommoditysQuery query = new CommoditysQuery();
		query.setId(id);
		List<Commoditys> list = commoditysService.findPage(query).getResult();
		if(list.size()>0) {
			entity=list.get(0);
		}
		render(companyInfo,entity,result);
	}
	
	
	public static void news_d(int id) {
		News  entity=new News();
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
	
		
		//获取新闻
		NewsQuery query = new NewsQuery();
		query.setId(id);
		List<News> list = newsService.findPage(query).getResult();
		
		if(list.size()>0) {
			entity=list.get(0);
		}
		render(companyInfo,entity);
	}
	

	
	public static void news() {
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		NewsQuery query =new NewsQuery();
		query.setIsShow("1");
		Page page = newsService.findPage(query);
		
		
		render(page,companyInfo);
	}

	/**
	 * 查询商品列表
	 */
	public static void product() {
		//读取公司信息
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		// 获取所有的类型
		MltwoLevelQuery query1 = new MltwoLevelQuery();
		List<MltwoLevel> result = mltwoLevelService.findPage(query1).getResult();

		for (MltwoLevel mltwoLevel2 : result) {
			CommoditysQuery query = new CommoditysQuery();
			query.setTypes(mltwoLevel2.getId());
			List<Commoditys> list = commoditysService.findPage(query).getResult();
			HashSet<Commoditys> hashSet = new HashSet<>();
			hashSet.addAll(list);
			mltwoLevel2.setCommodityss(hashSet);

		}
		
		renderArgs.put("IMG_PATH", IMG_PATH);
		renderArgs.put("result", result);
		renderArgs.put("companyInfo", companyInfo);
		render();

	}
	
	/**
	 * email 发送
	 */
	public static void  SendEmail(MessagesQuery mQuery){
		String companyJson = common.JsonFile.jsonUtil.ReadFile(COMPANYPATH);
		JSONObject companyInfo=JSONObject.parseObject(companyJson);
		Object object = companyInfo.get("companyEmail");
		
		boolean flag=false;
		try {//    邮箱
			Email email = new SimpleEmail();
			email.setHostName("smtp.qq.com");
			email.setSmtpPort(465);
			email.setAuthenticator(new DefaultAuthenticator("47323107@qq.com", "qznlxpokjvxgbgff"));
			email.setSSLOnConnect(true);
			email.setFrom("47323107@qq.com");
			email.setSubject(mQuery.getName()+"客户");
			email.setMsg("客户姓名："+mQuery.getName()
			+"\n客户电话："+mQuery.getPhone()
			+"\n客户邮箱："+mQuery.getEmail()
			+"\n客户内容："+mQuery.getMessages());
			email.addTo(object.toString());
			email.send();
			flag=true;
			// 邮件发送成功  将数据保存到数据库
			Messages entity=new Messages();
			entity.setName(mQuery.getName());
			entity.setPhone(mQuery.getPhone());
			entity.setEmail(mQuery.getEmail());
			entity.setMessages(mQuery.getMessages());
			messagesService.save(entity);
		} catch (EmailException e) {
			e.printStackTrace();
		}
		renderText(flag);
	}
}